﻿using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;

public static class ColliderExtension
{
    public static Vector3 GetColliderPoint(this Collider co, Vector3 position)
    {
        Transform transform = co.transform;
        return (transform.TransformPoint(Vector3.Scale(position, co.GetOBBExtents())) + (co.bounds.center - transform.position));
    }

    public static Vector3 GetOBBExtents(this Collider collider)
    {
        if (collider is CharacterController)
        {
            CharacterController controller = (CharacterController) collider;
            return new Vector3(controller.radius, controller.height * 0.5f, controller.radius);
        }
        if (collider is CapsuleCollider)
        {
            CapsuleCollider collider2 = (CapsuleCollider) collider;
            if (collider2.direction == 0)
            {
                return new Vector3(collider2.height * 0.5f, collider2.radius, collider2.radius);
            }
            if (collider2.direction == 1)
            {
                return new Vector3(collider2.radius, collider2.height * 0.5f, collider2.radius);
            }
            if (collider2.direction == 2)
            {
                return new Vector3(collider2.radius, collider2.radius, collider2.height * 0.5f);
            }
        }
        if (collider is BoxCollider)
        {
            BoxCollider collider3 = (BoxCollider) collider;
            return (Vector3) (collider3.size / 2f);
        }
        if (collider is SphereCollider)
        {
            SphereCollider collider4 = (SphereCollider) collider;
            return new Vector3(collider4.radius, collider4.radius, collider4.radius);
        }
        return Vector3.zero;
    }

    public static bool LinecastDetect(this Collider co, Vector3 startColliderPoint, Vector3 endColliderPoint)
    {
        RaycastHit hit;
        return co.LinecastDetect(startColliderPoint, endColliderPoint, out hit, Color.white);
    }

    public static bool LinecastDetect(this Collider co, Vector3 startColliderPoint, Vector3 endColliderPoint, int layerMask)
    {
        RaycastHit hit;
        return co.LinecastDetect(startColliderPoint, endColliderPoint, out hit, layerMask, Color.white);
    }

    public static bool LinecastDetect(this Collider co, Vector3 startColliderPoint, Vector3 endColliderPoint, out RaycastHit hit)
    {
        return co.LinecastDetect(startColliderPoint, endColliderPoint, out hit, -1, Color.white);
    }

    public static bool LinecastDetect(this Collider co, Vector3 startColliderPoint, Vector3 endColliderPoint, Color debugLineColor)
    {
        RaycastHit hit;
        return co.LinecastDetect(startColliderPoint, endColliderPoint, out hit, debugLineColor);
    }

    public static bool LinecastDetect(this Collider co, Vector3 startColliderPoint, Vector3 endColliderPoint, out RaycastHit hit, int layerMask)
    {
        return co.LinecastDetect(startColliderPoint, endColliderPoint, out hit, layerMask, Color.white);
    }

    public static bool LinecastDetect(this Collider co, Vector3 startColliderPoint, Vector3 endColliderPoint, out RaycastHit hit, Color debugLineColor)
    {
        return co.LinecastDetect(startColliderPoint, endColliderPoint, out hit, -1, debugLineColor);
    }

    public static bool LinecastDetect(this Collider co, Vector3 startColliderPoint, Vector3 endColliderPoint, out RaycastHit hit, int layerMask, Color debugLineColor)
    {
        Vector3 colliderPoint = co.GetColliderPoint(startColliderPoint);
        Vector3 end = co.GetColliderPoint(endColliderPoint);
        Debug.DrawLine(colliderPoint, end, debugLineColor);
        bool flag = Physics.Linecast(colliderPoint, end, out hit, layerMask);
        if (hit.collider == co)
        {
            flag = false;
        }
        return flag;
    }
}

